﻿/* 
The MIT License (MIT)

Copyright (c) 2013 Tilo Szepan, Immo Wache

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, 
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using WPDialog;

namespace WPDialogSample
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            var b = new Button();
            b.Content = "Start Demo";
            Grid.SetRow(b, 2);
            ContentPanel.Children.Add(b);
            b.Click += new RoutedEventHandler(b_Click);
        }

        void b_Click(object sender, RoutedEventArgs ea)
        {
            var root = new RootElement("WP Dialog"){
                new SeparatorElement("Examples for each Element"),
                new RootElement("All Elements"){
                    new SeparatorElement("a simple Element:"){ Alignment = TextAlignment.Left },
                    new Element("Element"),
                    new SeparatorElement("Date / Time"),
                    new DateElement(),
                    new TimeElement(),
                    new SeparatorElement("Toggle Buttons"),
                    new CheckElement("CheckElement:", true),
                    new SwitchElement("ToggleSwitch", true),
                    new SwitchElement("ToggleSwitch", false),
                    new RadioElement("Toggle RadioElement"){ Checked = true },
                    new RadioElement("uncolored Toggle RadioElement"){ UseAccentColor = false, Checked = true },
                    new SeparatorElement("EntryElements"),
                    new EntryElement("EntryElement:", "Text"){ SplitHorizontal = true },
                    new EntryElement("Password:", "password"){ IsPassword = true, SplitHorizontal = true },
                    new EntryElement("EntryElement:", "more Text"),
                    new EntryElement("Password:", "password"){ IsPassword = true },
                    new SeparatorElement("SliderElement"),
                    new SliderElement("Slider", 2),
                    new SliderElement(true, 7),
                    new SeparatorElement("HtmlElements:"){ Alignment = TextAlignment.Left },
                    new LinkElement("http://www.google.de"),
                    new HtmlElement("http://www.google.de")
                },
                new SeparatorElement("more detailed samples"),
                new RootElement("Samples"){
                    new SeparatorElement("RadioElements"),
                    RadioExamples(),
                    new SeparatorElement("EntryElements"),
                    EntryExamples(),
                    new SeparatorElement("Actions"),
                    ActionExamples(),
                    new SeparatorElement("Reflection"),
                    new Element("Reflection Demo", (e)=>{
                        var settings = new Settings()
                        {
                            AccountEnabled = true,
                            Login = "admin@localhost.com",
                        };
                        var bc = new BindingContext(settings, "Settings");
                        bc.Root.Push();
                    })
                }
            };

            root.Push(NavigationService);
        }
        RootElement RadioExamples()
        {
            var root = new RootElement("Radio Examples");
            var g1 = new RadioGroup(1) { "1. Item", "2. Item", "3. Item", "4. Item" };
            root.Add(g1, "RadioGroup", "End of RadioGroup");
            var g2 = new RadioGroup(0) { UseAccentColor = false };
            g2.Add("1. Item");
            g2.Add("2. Item");
            root.Add(g2, "uncolored RadioGroup", "End of RadioGroup");
            root.Add(new SeparatorElement("ungrouped RadioElements"));
            root.Add(new RadioElement("Toggle Button") { Checked = true });
            root.Add(new RadioElement("Toggle Button uncolored") { UseAccentColor = false, Checked = true });
            root.Add(new SeparatorElement("End RadioElements", SeparatorStyle.Footer));
            root.SetDefaultApplicationBar((s, arg) => { root.Title = g2[g2.Selected]; root.Pop(); }, (s, arg) => { root.Pop(); });
            return root;
        }
        RootElement EntryExamples()
        {
            var root = new RootElement("different entry styles"){
                   new SeparatorElement("Split horizontal"),
                   new EntryElement("Name:", "Tilo"){ SplitHorizontal = true },
                   new EntryElement("Country:", "Germany") { SplitHorizontal = true },
                   new EntryElement("Location:", "Berlin"){ SplitHorizontal = true },
                   new EntryElement("Password:", "password", true){ SplitHorizontal = true },
                   new SeparatorElement("vertical alignment"),
                   new EntryElement("Name:", "Tilo"),
                   new EntryElement("Country:", "Germany"),
                   new EntryElement("Location:", "Berlin"),
                   new EntryElement("Password:", "password"){ IsPassword = true }
            };
            return root;
        }
        RootElement ActionExamples()
        {
            Action<Element> action = (e) => { e.Parent.Page.Title = e.Title; };
            var root = new RootElement("Actions") {
                new SeparatorElement("Touch an element"),
                new Element("Element 1", action),
                new Element("Element Push", (e)=>{ new RootElement("Push / Pop") { new Element("Element Pop", (el)=>{ el.Parent.Pop(); }) }.Push(); }),
                InsertRemove(),
                new SliderElement(true, 0){ Action = (e)=>{ e.Parent.Page.Title = "Value: " + Math.Round((e as SliderElement).Value, 2);}},
                new CheckElement("Checked", false){ Action = (e)=>{ e.Parent.Page.Title = e.Title + ": " + (e as CheckElement).Checked; }},
                new SwitchElement("Switch", true){ Action = (e)=>{ e.Parent.Page.Title = e.Title + ": " + ((e as SwitchElement).Value ? "On" : "Off"); }},
                new RadioElement("Toggle"){ Action = (e)=>{ e.Parent.Page.Title = e.Title + ": " + (e as RadioElement).Checked; }}
            };
            var g = new RadioGroup(1) { "1. Item", "2. Item", "3. Item" };
            g.SelectionChanged = (e) =>
            {
                e.Parent.Page.Title = e.Parent.Title = "Radio: " + e.Title;
            };
            root.Add(g, "RadioGroup", "End of RadioGroup");

            return root;
        }
        RootElement InsertRemove()
        {
            int idx = 0;
            var root = new RootElement("Insert / Remove"){
                new SeparatorElement("Touch an element"),
                new Element("Insert", (e)=>{
                    e.Parent.InsertAt(new SeparatorElement("just inserted"), 3);
                    for(int i = 4; i < 7; i++) e.Parent.InsertAt(new Element("Inserted: " + ++idx), i);
                }),
                new Element("Remove", (e)=>{ for(int i = 0; i < 4 && e.Parent.Count > 3; i++) e.Parent.RemoveAt(3); })
            };
            return root;
        }
    }
}